﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
namespace BitPortal.Business
{
	public static class NoteWorkBLL
	{
		public static void CreateNoteWork(NoteWork noteWork)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			extBitPortal.NoteWorks.AddObject(noteWork);
			extBitPortal.SaveChanges();
		}

		public static void DeleteNoteWork(Int64 noteWorkId)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			NoteWork note = extBitPortal.NoteWorks.SingleOrDefault(pp => pp.NoteID == noteWorkId);
			extBitPortal.NoteWorks.DeleteObject(note);
			extBitPortal.SaveChanges();
		}

		public static void UpdateNoteWork(NoteWork noteWork)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			NoteWork note = extBitPortal.NoteWorks.SingleOrDefault(pp => pp.NoteID == noteWork.NoteID);
			note = noteWork;
			extBitPortal.SaveChanges();
		}


		public static List<NoteWork> GetAll()
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			IQueryable<NoteWork> listNote=from pp in extBitPortal.NoteWorks
                                          orderby pp.NoteID descending, pp.WorkWarrantyID descending
										  select pp;
			return listNote.ToList();
		}

		public static List<NoteWork> GetByWorkWarranty(Int64 workId)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			IQueryable<NoteWork> listNote=from pp in extBitPortal.NoteWorks
										  where pp.WorkWarrantyID==workId
										  select pp;
			return listNote.ToList();
		}

		public static List<NoteWork> GetByUser(Int64 userId)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			IQueryable<NoteWork> listNote=from pp in extBitPortal.NoteWorks
										  where pp.UserId.ToInt64TryParse(0)==userId
										  select pp;
			return listNote.ToList();
		}

        public static int DeletePermanently(long noteID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            NoteWork note = bitEnt.NoteWorks.SingleOrDefault(a => a.NoteID == noteID);
            if (note != null)
            {
                
                bitEnt.NoteWorks.DeleteObject(note);
              
                return bitEnt.SaveChanges();
            }
            return 0;
        }
    }
}